﻿using System.Text.Json;

namespace PBIW.Core
{
    internal sealed class Settings
    {
        #region Works
        private static readonly JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
        {
            AllowTrailingCommas = true,
            DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never,
            IgnoreReadOnlyFields = true,
            IgnoreReadOnlyProperties = true,
            IncludeFields = false,
            NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.Strict,
            ReadCommentHandling = JsonCommentHandling.Skip,
            WriteIndented = true
        };
        private static readonly JsonWriterOptions writerOptions = new JsonWriterOptions()
        {
            Indented = true,
            // SkipValidation = true
            SkipValidation = true
        };

        public string? FilePath { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="Exception"></exception>
        public void SaveSettings()
        {
            var file = new FileInfo(this.FilePath!);
            file!.Directory!.Create();
            using var stream = file.Create();
            Utf8JsonWriter writer = new Utf8JsonWriter(stream, writerOptions);
            JsonSerializer.Serialize(writer, this, serializerOptions);
        }

        public static Settings ReadSettings(string applicationId, string rootPathIfAppDataNotFound)
        {
            try
            {
                string appData;
                try
                {
                    appData = Environment.GetFolderPath(
                       Environment.SpecialFolder.LocalApplicationData,
                       Environment.SpecialFolderOption.Create);
                }
                catch
                {
                    appData = string.Empty;
                }

                if (appData == string.Empty)
                {
                    appData = Path.Combine(rootPathIfAppDataNotFound, "AppData");
                }

                var path = Path.Combine(applicationId.Split('.'));
                path = Path.Combine(appData, path);
                path = Path.Combine(path, "settings.json");

                var file = new FileInfo(path);
                path = file.FullName;

                if (!file.Exists)
                    return new Settings() { FilePath = path };

                using var stream = file.OpenRead();
                var settings = JsonSerializer.Deserialize<Settings>(stream, serializerOptions);
                var result = settings is null ? new Settings() : settings;
                result.FilePath = path;
                return result;
            }
            catch
            {
                return new Settings()
                {
                    FilePath = null
                };
            }
        }

        public Settings() { }
        #endregion

        #region Settings
        public bool GenerateOverlappingByChangedForQianAndKun { get; set; } = true;
        public bool EnableAutoCalculation { get; set; } = true;
        public bool EnableZhouyiReferencing { get; set; } = true;
        #endregion
    }
}
